// pages/details/details.js
let id = ''
let goodID
let _id=''
let cartID = ''
let OPENID = ''
let good = ''
let num = 0
let select = true
var GD = {_id,num,select}
//------------------
let sellerID=''
let goodName=''
let goodPrice=''
let userID=''
let sellerNAME=''
let goodimg=''
let distribution = '' //配送方式
let add = ''
let unit = ''  //单位
let inventory = '' //库存
let phone = ''
Page({
  data: {
    good:[],
    add:null
  },
  onLoad(options) {
    this.getopenid()
      console.log('列表携带的值',options)
      goodID = options.id 
      id = options.id
      // _id = options.id
      //doc查询单条数据,商品详情页调这个
      wx.cloud.database().collection('send-Goods')
      .doc(id)
      .get()
      .then(res=>{
       //查询成功
       console.log('单条查询成功',res)
       sellerID=res.data.openID
       goodName=res.data.name
       goodPrice=res.data.price
       sellerNAME=res.data.NAME
       goodimg=res.data.imgpath[0].url
       distribution = res.data.distribution //配送方式
       add = (res.data.province+'-'+res.data.city+'-'+res.data.district)
       unit = res.data.unit  //单位
       inventory = res.data.inventory //库存
      phone = res.data.phone
       this.setData({
         good:res.data,
         add:add
       })
      })
      .catch(res=>{
       //查询失败
       console.log('单条查询失败',res)
      })
  },
  contact(){
    wx.showModal({
      title: '商家联系方式',
       content: phone,
       confirmText:'确定',
       cancelText:'返回',
       success: (res) =>{
         if (res.confirm) {
             //点击符合按钮
         } else if (res.cancel) {
             //点击不符合按钮
         }
       }
   })
  },
getshoppingCart(OPENID){
    wx.cloud.database().collection('shopping-cart')
    .where({
        openid:OPENID,
    })
    .get()
    .then(res=>{
      console.log('商品id列表获取成功',res)
      cartID = res.data[0]._id
    })
    .catch(err=>{
      console.log('条件查询失败',err)
    })
},
  addShoppingCart(){
    if(sellerID==userID){
      wx.showToast({
        title: '不能加购自己的商品',
        icon:'none'
      })
    } else {
    var IS = false
    wx.cloud.database().collection('shopping-cart')
    .where({
        openid:OPENID,
    })
    .get()
    .then(res=>{
      console.log('购物车查询成功',res)
      let changdu = res.data[0].goods.length
      for (let AA=0; AA < changdu; AA++) {
      let ID = res.data[0].goods[AA].id;
      cartID = res.data[0]._id
        if(ID==goodID)
        {
          IS = true
          wx.showToast({
            title: '商品已存在,数量加一',
            icon:'none'
          })
         this.changeNum(3,AA,cartID,parseInt(1))
         console.log('商品存在,云函数加一成功')
        }
      }
      if(IS==false){
        wx.cloud.callFunction({
          name:'addCart',
          data:{
            id:id,
            num:1,
            select:true
          }
        })
         .then(res=>{
           console.log('商品不存在,云函数加入购物车成功',res)
           wx.showToast({
             title: '加入购物车成功',
             icon:''
           })
         })
         .catch(err=>{
           console.log('商品不存在,云函数加入购物车失败',err)
         })
      }
    })
    .catch(err=>{
      console.log('商品属性查询失败',err)
    })    
    }
  },
   //修改num值
 changeNum(willNum,index,Cartid,type){
  wx.cloud.callFunction({
    name:'changeCartNum',
    data:{
      WillNum:willNum,
      index:index,
      cartID:Cartid,
      Type:type
    }
  }).then(res=>{
   console.log('文本框修改成功：',res)
 })
 .catch(err=>{
   console.log('文本框修改失败：',err)
 })
},
////////////////////////////
  Buy(){
    if(sellerID==userID){
      wx.showToast({
        title: '不能购买自己的商品',
        icon:'none'
      })
    } else {
    wx.navigateTo({
      url: '/pages/PurchaseInformation/PurchaseInformation?sellerID='+sellerID+'&goodName='+goodName+'&goodPrice='+goodPrice+'&userID='+userID+'&sellerNAME='+sellerNAME+'&goodimg='+goodimg+'&goodID='+id+'&distribution='+distribution+'&add='+add+'&unit='+unit+'&inventory='+inventory+'&phone='+phone,
    })
  }
  },
  goShoppingCart(){
    wx.navigateTo({
      url: '/pages/ShoppingCart/ShoppingCart?openid='+id
      // ?type1='+Type1+'&type2='+Type2
    })
  },
  getopenid(){
    wx.cloud.callFunction({
      name:'getOPENID'
    })
    .then(res=>{
      this.setData({
        OPENID:res.result.openid
      })
      OPENID = res.result.openid
      console.log('成功获取USERopenID',OPENID)
      userID=OPENID
      this.getshoppingCart(OPENID)
    })
  },
  goSeller(){
    wx.navigateTo({
      // url: '/pages/details-sellerinformation/details-sellerinformation?openid='+id
      url: '/pages/details-sellerinformation/details-sellerinformation?sellerID='+sellerID
      // ?type1='+Type1+'&type2='+Type2
    })
  }
})